import { ssoLogin } from "@/services/login.service";
import { USERTOKEN } from "@/types/constants/user-token.constant";
import { UserTokenModel } from "@/types/models/user-token.model";
import { useCallback, useState } from "react";
import { history } from "umi";
const getUserToken = (): UserTokenModel | null => {
    const data = window.sessionStorage.getItem(USERTOKEN)
    if (!data) {
        return null
    } else {
        return JSON.parse(data)
    }
}
const cacheUserToken = (token: UserTokenModel) => { }
export default function useAuthModel() {
    const [userToken, setUserToken] = useState<UserTokenModel | null>(getUserToken())
    const login = useCallback(async (code: string) => {
        try {
            const loginRes = await ssoLogin(code)
            setUserToken(loginRes);
            cacheUserToken(loginRes);
        } catch (e) {
            console.error(e)
            history.push('/error?code=403')
        }
    }, [])
    return{
        userToken,
        login
    }
}